<?php

namespace app\store\controller;
use \Workerman\Worker;
use \Workerman\Lib\Timer;
use think\Db;
use think\Log;
use Predis\Client;
/**
 *  客户分配
 */
class Timers{

}
$task = new Worker();
$task->onWorkerStart = function ($task) {
    $time_interval = 60;
    Timer::add($time_interval, function () {
        //获取买、求租/跟进人id为空客户/楼管录入/的客户信息
        $toBeAssignedInfos=Db::name('love_customer')->where(['datatype'=>['in','1,4'],'floweuserid'=>['<',1]])
            ->field('id,datatype,loupanid,floweuserid,iszilu,cityid,areaid')->select()->toArray();
        if(empty($toBeAssignedInfos)){
            Log::mylog('未获取到客户日志(log)：','未获取到客户信息','fen-nocustomer');
            return false;
        }
        //分配客户
        foreach($toBeAssignedInfos as $k=>$v){
            //根据待分配客户的意向区域获取对应销售组织账号
            $new=[];
            $userInfo=Db::name('love_admin_user')->where(['areaid'=>$v['areaid'],'type'=>['in','4,5'],'is_delete'=>0])
                ->field('id,type,loupanids')->select()->toArray();
            foreach ($userInfo as $kk=>$vv){
                if($vv['type']==4){
                    $s=explode(',',$vv['loupanids']);
                    if(in_array($v['loupanid'],$s)){
                        $new[]=$vv;
                    }
                }else{
                    if($v['loupanid']==$vv['loupanids']){
                        $new[]=$vv;
                    }
                }
            }
            if(!$new){
                Log::mylog('未获取到销售人员(log)：','未获取到销售人员','fen-nouser');
                continue;
            }else{
                //构建待分配数据池 //锁定本次被分配人id
                $id=0;
                $length=count($new);
                $newarray=[];
                foreach ($new as $k11=>$v11){
                    $newarray[]=$v11['id'];
                }
                $flag=0;
                foreach ($new as $k1=>$v1){
                    //查询上一次对应组织被分配人id
                    $logInfo=Db::name('love_distribution_log')->where(['areaid'=>$v['areaid']])->order('id desc')->limit(1)->find();
                    if($logInfo){
                        $keys=array_search($logInfo['userid'], $newarray);
                        if($keys==$length-1){
                            $id=$new[0]['id'];
                        }else{
                            $id=$new[$keys+1]['id'];
                        }
                    }else{
                        $id=$new[0]['id'];
                    }
                    if(!$flag){
                        $flag=$id;
                    }
                    Log::mylog('测试日志：','被分配的销售id:'.$id,'fen-fenpeiCustomer');
                    $where['visit_time']=[['=',0],['<',time()],'or'];
                    $where['inputuserid']=['<>',$id];
                    $where['floweuserid']=['=',$id];
                    $where['status']=['=',1];
                    $count=Db::name('love_customer')->where($where)->count();
                    if($count>=3){
                        Log::mylog('测试日志：','不符合的id:'.$id,'fen-bufuhe');
                        $data['areaid']=$v['areaid'];
                        $data['userid']=$id;
                        $data['customerid']=$v['id'];
                        $data['create_time']=time();
                        $data['status']=2;
                        Db::name('love_distribution_log')->insert($data);
                        $xx=0;
                        foreach ($newarray as $kk=>$vv){
                            if($newarray[0]==$flag){
                                $xx=$newarray[count($newarray)-1];
                            }else{
                                if($flag==$vv){
                                    $xx=$newarray[$kk-1];
                                }
                            }
                        }
                        if($id==$xx){
                            Log::mylog('测试日志：','一轮结束id:'.$id,'test-zd');
                            break;
                        }else{
                            Log::mylog('测试日志：','不符合，进行下一条数据判断，不符合id:'.$id,'test-next');
                            continue;
                        }
                    }else{
                        //如果符合完成分配 //修改客户表，写入分配日志、流程日志 跳出分配
                        Log::mylog('测试日志：','被更新的销售id:'.$id,'fen-updateCustomer');
                        //发送订阅消息 ，将客户id  跟进人id写入redis  todo
                        $server=[
                            'host' => '127.0.0.1',
                            'port' => 6379,
                        ];
                        $redis= new Client($server);
                        //$inputData['customerid']=$v['id'];
                        //$inputData['userid']=$id;
                        $pu=$v['id'].'_'.$id;
                        $r=$redis->rpush('customerData2',$pu);
                        Log::mylog('测试日志：','redis:'.$r,'fen-redis');
                        $updateCustomer=Db::name('love_customer')->where(['id'=>$v['id']])->update(['floweuserid'=>$id,'fenpei_time'=>time()]);
                        $data['areaid']=$v['areaid'];
                        $data['userid']=$id;
                        $data['customerid']=$v['id'];
                        $data['create_time']=time();
                        $data['status']=1;
                        $data['loupanid']=$v['loupanid'];
                        Db::name('love_distribution_log')->insert($data);
                        //写入操作日志
                        $logdata['userid']=0;
                        $logdata['datatype']=8;
                        $logdata['fenpeiid']=$id;
                        $logdata['customerid']=$v['id'];
                        $logdata['fromType']=2;
                        $logdata['remark']='系统分配';
                        $logdata['create_time']=time();
                        Db::name('love_log')->insert($logdata);
                        break;
                    }
                }
            }
        }
    });
};
//运行worker
Worker::runAll();